package com.jzy.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jzy
 * @date 2020/9/22 14:11
 */
@Configuration
public class MyRabbitBean {

    /*@RabbitListener(queues = "order.release.order.queue")
    public void listen(OrderEntity orderEntity){
        System.out.println("收到过期的订单消息:准备关闭订单");
    }*/


    //延迟队列  支付延时60秒 订单延时70秒 库存延时80秒
    @Bean
    public Queue orderDelayQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //			@Nullable Map<String, Object> arguments
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 70000);
        return new Queue("order.delay.queue", true, false, false, arguments);
    }

    //死信队列
    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue", true, false, false);
    }

    //order服务交换机
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Binding orderCreateOrder() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			@Nullable Map<String, Object> arguments
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
    }

    @Bean
    public Binding orderReleaseOrder() {
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
    }

    //订单解锁之后发送消息库存,防止库存在订单之前触发解锁
    @Bean
    public Binding orderReleaseWare() {
        return new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.ware", null);
    }

    //秒杀队列
    @Bean
    public Queue seckillOrderQueue() {
        return new Queue("seckill.order.queue", true, false, false);
    }

    //绑定秒杀队列
    @Bean
    public Binding seckillOrderBinding() {
        return new Binding("seckill.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "seckill.create.order", null);
    }
}
